package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begintime, LocalDateTime endtime) {

        Integer newusers = userMapper.getNnmberNewUsers(begintime,endtime);
        newusers = newusers ==null?0:newusers;

        Double orderCompletionRate =  ordersMapper.getCompletionRate(begintime,endtime);
        orderCompletionRate = orderCompletionRate ==null?0:orderCompletionRate;

        Double turnover = ordersMapper.getTurnover(begintime,endtime);
        turnover = turnover==null?0:turnover;

        Double unitPrice = ordersMapper.getUnitPrace(begintime,endtime);
        unitPrice = unitPrice ==null?0:unitPrice;

        Integer validOrderCount = ordersMapper.getValidOrderCount(begintime,endtime);
        validOrderCount = validOrderCount ==null?0:validOrderCount;

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .newUsers(newusers)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();

        return businessDataVO;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate now = LocalDate.now();
        LocalDateTime begintime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime endtime = LocalDateTime.of(now, LocalTime.MAX);

        HashMap map = new HashMap<>();
        map.put("begintime",begintime);
        map.put("endtime",endtime);
        map.put("status",null);
        Integer allOrders = ordersMapper.getNumberByStatusAndTime(map);
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.getNumberByStatusAndTime(map);
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = ordersMapper.getNumberByStatusAndTime(map);
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = ordersMapper.getNumberByStatusAndTime(map);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = ordersMapper.getNumberByStatusAndTime(map);

        OrderOverViewVO orderOverViewVO = OrderOverViewVO
                .builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();

        return orderOverViewVO;
    }
}
